package com.wk.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wk.dao.CmfzAdminDao;
import com.wk.entity.CmfzAdmin;
import com.wk.entity.CmfzAdminRole;
import com.wk.entity.Result;
import com.wk.service.CmfzAdminRoleService;
import com.wk.service.CmfzAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private CmfzAdminService cmfzAdminService;
    @Autowired
    private CmfzAdminRoleService cmfzAdminRoleService;


    @RequestMapping("login")
    public Map login(String code, HttpSession session, CmfzAdmin cmfzAdmin){
        Map map = new HashMap();

        String realCode = (String) session.getAttribute("realCode");
        if(!realCode.equalsIgnoreCase(code)){
                map.put("status",0);
                map.put("data","验证码错误");
                return map;
        }
        String username = cmfzAdmin.getUsername();
        String password = cmfzAdmin.getPassword();

//        1.将数据封装到token中
        UsernamePasswordToken token = new UsernamePasswordToken(cmfzAdmin.getUsername(), cmfzAdmin.getPassword());
//        2.获取主体
        Subject subject = SecurityUtils.getSubject();
//        3.通过主体完成认证
        try {
            subject.login(token);
            System.out.println("成功");
            map.put("status",1);
            map.put("data","登录成功");
            return map;

        }catch (Exception e){
            System.out.println("失败");
            map.put("status",2);
            map.put("data","用户名或密码错误");
            return map;
        }


    }


    @RequestMapping("findAll")
    public Map findAll(Integer page,Integer limit){
        if(SecurityUtils.getSubject().isPermitted("admin:select")){
           return cmfzAdminService.findByPage(page,limit);
        }

        return null;

    }


    @RequiresRoles("superadmin")
    @RequestMapping("remove")
    public Map remove(Integer id){
        return cmfzAdminService.remove(id);
    }

//    @RequiresRoles("superadmin")
//    @RequestMapping("add")
//    public Map add(CmfzAdmin cmfzAdmin){
//
//        return cmfzAdminService.add(cmfzAdmin);
//    }

    @RequestMapping("add")
    public Result add(CmfzAdminRole cmfzAdminRole){

        Result result = new Result();

        try {
            cmfzAdminService.add(cmfzAdminRole);
            result.setData("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setData("添加失败");
        }
        return result;
    }

    @RequiresRoles("superadmin")
    @RequestMapping("findOne")
    public Map findOne(Integer id){
        return cmfzAdminService.findOne(id);
    }

    @RequiresRoles("superadmin")
    @RequestMapping("update")
    public Map update(CmfzAdmin cmfzAdmin,CmfzAdminRole cmfzAdminRole){
        return cmfzAdminService.update(cmfzAdmin,cmfzAdminRole);
    }
}
